<?php

namespace App\Http\Controllers\Home;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RegisterController extends Controller
{
   public function index()
   {
      return view('home.register');
    }
    public function register(Request $request)
    {
//        dd($request->all());
        $this->validate($request,[
            'email'=>'email|unique:users',
            'password'=>'required|confirmed',
            'code'=>'required',
            'address'=>'required',
            'phone'=>'required',
            'name'=>'required',
        ],[
            'email.email' => '邮箱格式错误',
            'email.unique' => '邮箱已经存在',
            'password.required' => '密码不能为空',
            'password.confirmed' => '两次密码不一致',
            'code.required' => '验证码不能为空',
            'name.required' => '用户名不能为空',
            'address.required' => '地址不能为空',
            'phone.required' => '联系方式不能为空',
        ]);
        //      验证码和邮箱的比较
        $status = session('register.email') == $request->input('email') && session('register.code')
            == $request->input('code');
        if (!$status){
            return back()->with('error','验证码错误');
        }
        $data = $request->all();
        $data['password'] = bcrypt($data['password']);
        User::create($data);
        return redirect(route('home.login'))->with('success','注册成功，请登录账号');
    }
}
